package com.android.SchoolManagerSystem.action;

import java.net.HttpURLConnection;
import java.util.HashMap;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.android.SchoolManagerSystem.CommonInfo;
import com.android.SchoolManagerSystem.data.Curricula;
import com.android.SchoolManagerSystem.data.InternShip;
import com.android.SchoolManagerSystem.data.Userinfo;
import com.android.SchoolManagerSystem.net.HttpRequester;
import com.android.SchoolManagerSystem.net.HttpResponser;

public class GetInternShip {
	private Userinfo 	curinfo = null;
	private InternShip 	tmpInterShip = null;
	private String 		srtSemester = null;
	private boolean 	berrorcode = false;

	public final static int OPER_QUERY = 0;
	public final static int OPER_MODIFY = OPER_QUERY + 1;

	public GetInternShip(Userinfo curuser, String srtSemesterin) {
		curinfo = curuser;
	}

	public boolean go(int arg) {
		// decodelogresponce(null);
		// return false;

		String strHosturi = CommonInfo.strGetTranscripturi;
		String strrequetstXMl = generatexmlstring(arg);

		switch (arg) {
		case OPER_QUERY:
		default:
			strHosturi = CommonInfo.strqueryinternshipuri;
			break;
		case OPER_MODIFY:
			strHosturi = CommonInfo.strupdateinternshipuri;
			break;
		}

		HashMap<String, String> propertys = new HashMap<String, String>();
		propertys.put("Set-Cookie", "ONE_VER=1_0;sid=10006");
		propertys.put("H-Product", "client");
		HttpRequester loginrequest = new HttpRequester();
		HttpResponser loginresponse = loginrequest.sendPost(strHosturi,
				propertys, strrequetstXMl);
		if (loginresponse == null
				|| loginresponse.getCode() != HttpURLConnection.HTTP_OK)
			return false;
		else {
			if (Decodesvrreponse(arg, loginresponse))
				return true;
			else
				return false;
		}
	}

	/**
	 * Send http request, and return http response
	 * 
	 * @author jeson_li
	 * @date 2011-3-28
	 */
	private boolean Decodesvrreponse(int arg, HttpResponser response) {
		boolean bres = false;
		switch (arg) {
		case OPER_QUERY:
			decodeCCulaList(response);
			break;
		case OPER_MODIFY:
		default:
			decodeUpdateRes(response);
			break;
		}

		return bres;
	}

	private String generatexmlstring(int arg) {
		String strlogreq = null;
		switch (arg) {
		case OPER_QUERY:
		default:
			strlogreq = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
					+ "<queryinternships apiversion=\"1.0\"><userid>"
					+ curinfo.struserid + "</userid><password>"
					+ curinfo.getuserpsw() + "</password></queryinternships>";
			break;

		case OPER_MODIFY:
			strlogreq = "<?xml version=\"1.0\" encoding=\"utf-8\"?><requestinternships  apiversion=\"1.0\"><curricula><userid>"
					+ curinfo.struserid + "</userid>";
			for (int i = 0; i < curinfo.MyCurricula.size(); i++) {
				Curricula cculatmp = curinfo.MyCurricula.get(i);
				if (cculatmp.bChoosed) {
					String strCulaNode = "<culaid>" + cculatmp.strCurriculaName
							+ "</culaid>";
					strlogreq += strCulaNode;
				}
			}
			strlogreq += "</curricula></requestinternships>";
			break;
		}

		return strlogreq;
	}

	private boolean decodeUpdateRes(HttpResponser repsonse) {

		String testxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><queryallscorer apiversion=\"1.0\"><ccula><culaid>math</culaid><credits>2</credits><mark>88</mark></ccula><ccula><culaid>english</culaid><credits>3</credits><mark>80</mark></ccula></queryallscorer>";
		RootElement root = new RootElement("queryallscorer");

		root.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
			}
		});
		root.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
			}
		});

		root.getChild("errorcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						if (body == "0")
							berrorcode = false;
						else
							berrorcode = true;
					}
				});

		try {
			Xml.parse(testxml, root.getContentHandler());
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		if (berrorcode)
			return false;
		else
			return true;
	}

	private boolean decodeCCulaList(HttpResponser repsonse) {

		String testxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><queryallscorer apiversion=\"1.0\"><ccula><culaid>math</culaid><credits>2</credits><mark>88</mark></ccula><ccula><culaid>english</culaid><credits>3</credits><mark>80</mark></ccula></queryallscorer>";
		RootElement root = new RootElement("queryallscorer");

		root.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
			}
		});
		root.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
			}
		});

		Element e = root.getChild("curricula");

		e.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				tmpInterShip = new InternShip();
			}
		});

		e.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
				curinfo.MyIntership.add(tmpInterShip);
			};
		});

		e.getChild("culaid").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							tmpInterShip.strJobName = body;
						}
					}
				});
		e.getChild("culaname").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							tmpInterShip.strJobName = body;
						}
					}
				});

		e.getChild("status").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							if (body == "1")
								tmpInterShip.iStatus = 1;
							else
								tmpInterShip.iStatus = 0;
						}
					}
				});

		e.getChild("errorcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						if("0" != body)
							berrorcode = true;
					}
				});

		try {
			Xml.parse(testxml, root.getContentHandler());
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		if (berrorcode)
			return false;
		else
			return true;
	}
}
